package com.yizhiliulianta.user.designPattern.filterPattern;

import java.util.List;
import java.util.stream.Collectors;

public class ArticleFilterDemo {

    private List<IArticleFilter> iArticleFilterList;

    private void init() {
        List<ArticleFilterEnum> filterList = ArticleFilterFactory.getFilterList();
        this.iArticleFilterList = ArticleFilterFactory.createArticleFilter(filterList);
    }

    public List<Article> doFileter(List<Article> articleList) {
        List<Article> collect = articleList.stream().filter(article -> {
            ArticleContext articleContext = new ArticleContext();
            articleContext.setArticle(article);
            return doFilter(articleContext);
        }).collect(Collectors.toList());
        return collect;
    }

    private boolean doFilter(ArticleContext articleContext) {
        init();
        for (IArticleFilter iArticleFilter : iArticleFilterList) {
            if (iArticleFilter.doFilter(articleContext)) {
                return false;
            }
        }
        return true;
    }

}
